/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package impro;
import ConnectData.ConfigServer;
import ConnectData.DataConnect;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
 *
 * @author MyLaptop
 */
public class Login extends javax.swing.JFrame {

    Connection Con;
    Statement Sta;
    ResultSet Res;
    public String Username;
    public String Roles; 
    
    public Login() {
        initComponents();
        
        jLabel_msUsername.setText("");
        jLabel_msPassword.setText("");
        jLabel_msRoles.setText("");
        Username = jTextField_Username.getText();

        try {
            Con = DataConnect.connectDB();
            Sta = Con.createStatement();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
     public void Login() {
      
        if (jTextField_Username.getText().equals("")) {
            jLabel_msUsername.setText("User name not null !");
            jTextField_Username.requestFocus();
            return;
        }
        if (jPasswordField_Password.getText().equals("")) {
            jLabel_msPassword.setText("Pass not null !");
            jPasswordField_Password.requestFocus();
            return;
        }
        if (jComboBox_Roles.getSelectedIndex() == 0) {
            jLabel_msRoles.setText("Choose a Roles !");
            return;
        }
        String Roles = "";
        if (jComboBox_Roles.getSelectedIndex() == 1) {
            Roles = "Admin";
            try {
                Res = Sta.executeQuery("Select *from Administrator where Admin_Username='" + jTextField_Username.getText().trim() + "' and Admin_Password='" + jPasswordField_Password.getText().trim() + "' and Admin_Permission='"+Roles+"'");
            if (Res.next()) {
                jLabel_msPassword.setText("");
                JOptionPane.showMessageDialog(null, "Login success !");
                this.dispose();
//                Main_GUI frmMainform = new Main_GUI();
//                frmMainform.setlable(jTextField_Username.getText().trim());
//                frmMainform.setroles( (String) jComboBox_Roles.getSelectedItem());
//                frmMainform.setVisible(true);
            }
            else
                jLabel_msPassword.setText(" User or pass invalidate !");
            } catch (Exception e) {
            }
     }
            else
                try {
                Res = Sta.executeQuery("Select *from Staff where Staff_Username='" + jTextField_Username.getText().trim() + "' and Staff_Password='" + jPasswordField_Password.getText().trim() + "' and Staff_Permission='"+jComboBox_Roles.getSelectedItem()+"'");
            if (Res.next()) {
                 jLabel_msPassword.setText("");
                JOptionPane.showMessageDialog(null, "Login success !");
                this.dispose();
//                Main_GUI frmMainform = new Main_GUI();
//                frmMainform.setlable(jTextField_Username.getText().trim());
//                frmMainform.setroles( (String) jComboBox_Roles.getSelectedItem());
//                frmMainform.setVisible(true);
            }
            else
              jLabel_msPassword.setText(" User or pass invalidate !");
            } catch (Exception e) {
            }

    }
    
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton_Login = new javax.swing.JButton();
        jComboBox_Roles = new javax.swing.JComboBox();
        jButton_Exit = new javax.swing.JButton();
        jLabel_msUsername = new javax.swing.JLabel();
        jLabel_msPassword = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel_msRoles = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPasswordField_Password = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jTextField_Username = new javax.swing.JTextField();
        jButton_Config = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(null);

        jButton_Login.setText("Login");

        jComboBox_Roles.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Select --", "Admin", "Sales_Manager", "Inventory_Manager", "Sales_person" }));

        jButton_Exit.setText("Exit");

        jLabel_msUsername.setForeground(new java.awt.Color(255, 0, 0));
        jLabel_msUsername.setText("Message");

        jLabel_msPassword.setForeground(new java.awt.Color(255, 0, 0));
        jLabel_msPassword.setText("Message");

        jLabel2.setText("User name :");

        jLabel_msRoles.setForeground(new java.awt.Color(255, 0, 0));
        jLabel_msRoles.setText("Message");

        jLabel4.setText("Roles :");

        jLabel3.setText("Password :");

        jButton_Config.setText("Config");
        jButton_Config.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ConfigActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/userlogin.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(88, 88, 88)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox_Roles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel_msRoles)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jPasswordField_Password, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextField_Username, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(80, 80, 80)
                        .addComponent(jButton_Login)
                        .addGap(34, 34, 34)
                        .addComponent(jButton_Config)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton_Exit)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_msUsername)
                    .addComponent(jLabel_msPassword))
                .addContainerGap(44, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(166, 166, 166))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jLabel1)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField_Username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_msUsername))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jPasswordField_Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_msPassword))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox_Roles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel_msRoles)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Exit)
                    .addComponent(jButton_Login)
                    .addComponent(jButton_Config))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_ConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ConfigActionPerformed
        ConnectData.ConfigServer a = new ConfigServer();
        a.setVisible(true);
    }//GEN-LAST:event_jButton_ConfigActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Config;
    private javax.swing.JButton jButton_Exit;
    private javax.swing.JButton jButton_Login;
    private javax.swing.JComboBox jComboBox_Roles;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel_msPassword;
    private javax.swing.JLabel jLabel_msRoles;
    private javax.swing.JLabel jLabel_msUsername;
    private javax.swing.JPasswordField jPasswordField_Password;
    private javax.swing.JTextField jTextField_Username;
    // End of variables declaration//GEN-END:variables
}
